package DAO;

import Entity.ICcard;
import Entity.InterTab;
import util.Result;
import util.RfidTool;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InterTabDao {
    private static short tsector = 1;
    private static short tblk = 6;

    static Connection connect = null;//数据库连接对象
    static PreparedStatement cmmd = null;//数据库命令执行对象
    static ResultSet rs = null;//数据集
    static String sql = "";


    RfidTool instance = RfidTool.getInstance();
    String cardID = instance.showCardId();//获取id卡的号码


    public String msg() {
        String msg = instance.getdeviceInti();
        System.out.println(msg);
        return msg;
    }

    public Result insert(InterTab in) {
        Result result = new Result();
        sql = "insert into InterTab values(?,?,?,?)";
        try {

            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);

            cmmd.setString(1, in.getInterID());
            cmmd.setString(2, in.getInterName());//传入参数的个数
            cmmd.setString(3, in.getInterTel());
            cmmd.setString(4, in.getInterLocat());
            cmmd.execute(); //执行sql语句

        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }


    public Result update3(InterTab in) {
        Result result = new Result();
        sql = "update InterTab set InterName= '" + in.getInterName() + "',InterTel= '" + in.getInterTel() + "',InterLocat= '" + in.getInterLocat() + "'where InterID = '" + in.getInterID() + "'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.execute();


        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;

    }

    public Result Delete(String out) {
        Result result = new Result();
        sql = "delete from InterTab where InterID='"+out+"'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            cmmd.execute(); //执行sql语句
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }

    public Result Querry(String x) {

        Result result = new Result();
        List<InterTab> list = new ArrayList<>();
        InterTab web;

        sql = "Select * from InterTab where InterID like '%" + x + "%' order by InterID+''";

        try {

            System.out.println(sql + "111");
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            // cmmd.setString(1,x);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                web = new InterTab();
                web.setInterID(rs.getString(1));
                web.setInterName(rs.getString(2));
                web.setInterTel(rs.getString(3));
                web.setInterLocat(rs.getString(4));
                list.add(web);
            }
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        result.setMessage(list);
        return result;


    }
    public String justgetID() throws SQLException {
        String id = null;
        sql = "SELECT InterID FROM  InterTab order by (InterID+0) DESC limit 1";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            rs = cmmd.executeQuery();
            while (rs.next()){
                id = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }
    public InterTab get_byID(String id){
        InterTab interTab = new InterTab();
        sql = "Select * from InterTab where InterID = '" + id + "'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                interTab.setInterID(rs.getString(1));
                interTab.setInterName(rs.getString(2));
                interTab.setInterTel(rs.getString(3));
                interTab.setInterLocat(rs.getString(4));

            }
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null,"没有获取到信息");
        }
        return interTab;
    }
}
